﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net.Mail;

namespace Alex.Library.Log.Configuration
{
    /// <summary>
    /// 邮件日志元素
    /// </summary>
    public sealed class MailLoggerElement : LoggerElement
    {
        /// <summary>
        /// 获取发件人元素
        /// </summary>
        [ConfigurationProperty("from", IsRequired = true)]
        public MailFromElement From
        {
            get
            {
                return (MailFromElement)this["from"];
            }
        }

        /// <summary>
        /// 收件人地址元素
        /// </summary>
        [ConfigurationProperty("to", IsRequired = true)]
        public AddressElementCollection To
        {
            get
            {
                return (AddressElementCollection)this["to"];
            }
        }

        /// <summary>
        /// 抄送地址元素
        /// </summary>
        [ConfigurationProperty("cc", IsRequired = true)]
        public AddressElementCollection CC
        {
            get
            {
                return (AddressElementCollection)this["cc"];
            }
        }

        /// <summary>
        /// 邮件标题
        /// </summary>
        [ConfigurationProperty("subject", IsRequired = true)]
        public StringContentElement Subject
        {
            get
            {
                return (StringContentElement)this["subject"];
            }
        }

        /// <summary>
        /// 邮件内容是否为HTML格式
        /// </summary>
        [ConfigurationProperty("isBodyHtml", IsRequired = false)]
        public BooleanContentElement IsBodyHtml
        {
            get
            {
                return (BooleanContentElement)this["isBodyHtml"];
            }
        }

        /// <summary>
        /// 邮件发送优先级
        /// </summary>
        [ConfigurationProperty("priority", IsRequired = false)]
        public MailPriorityContentElement Priority
        {
            get
            {
                return (MailPriorityContentElement)this["priority"];
            }
        }

        /// <summary>
        /// 邮件凭证
        /// </summary>
        [ConfigurationProperty("credential", IsRequired = true)]
        public MailCredentialElement Credential
        {
            get
            {
                return (MailCredentialElement)this["credential"];
            }
        }

        /// <summary>
        /// 根据配置获取邮件日志对象
        /// </summary>
        /// <returns></returns>
        public override ILogger GetLogger()
        {
            SmtpLogger sl = new SmtpLogger();
            sl.FlushLevel = this.FlushLevel.Value;
            sl.Template = this.Template.GetTemplate();
            foreach (AddressElement address in this.To)
            {
                sl.To.Add(address.Value);
            }
            foreach (AddressElement address in this.CC)
            {
                sl.CC.Add(address.Value);
            }
            sl.From = this.From.GetMailFromObject();
            sl.Subject = this.Subject.Value;
            sl.IsBodyHtml = this.IsBodyHtml.Value;
            sl.Priority = this.Priority.Value;
            sl.Credential = this.Credential.GetCredentialObject();
            return sl;
        }
    }
}
